package com.wjc.wjc.web;


import com.wjc.wjc.service.StudentService;
import com.wjc.wjc.unity.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;

@Controller
public class InforMyController {

    @Autowired
    private StudentService studentService;

    @GetMapping("/information")
    public String infor(){
        return "information";
    }


    @GetMapping("/updatepwd")
    public String pwd(){
        return "update-pwd";
    }

    @PostMapping("/update/pwd")
    public String upPwd(@RequestParam String sno,
                        @RequestParam String pwd,
                        @RequestParam String password,
                        RedirectAttributes attributes,
                        HttpSession session){

        Student s=studentService.login(sno,pwd);

        if (s==null){
            attributes.addFlashAttribute("message","原密码有误");
            return "redirect:/updatepwd";
        }
        else {
            studentService.updatePwd(password, sno);
            session.removeAttribute("student");
            session.removeAttribute("user");
            return "login";
        }

    }

    @GetMapping("/updateinfor")
    public String upIn(){
        return "information-input";
    }

    @PostMapping("/update/infor")
    public String updateInfor(@RequestParam String sex,
                        @RequestParam String birthDate,
                        @RequestParam String affiliation,
                              @RequestParam String address,
                              @RequestParam String sno,
                              HttpSession session){
        studentService.updateIfor(sex, birthDate, affiliation, address, sno);
        session.removeAttribute("student");
        Student s=studentService.getOne(sno);
        s.setPassword(null);
        session.setAttribute("student",s);

        return "redirect:/information";
    }
}
